package com.it.park.controller.user;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.it.park.client.service.ArticleTypeService;
import com.it.park.client.service.ResouceFileService;
import com.it.park.client.service.ResourceCollectionService;
import com.it.park.client.service.ResourceDownloadRecordService;
import com.it.park.common.SessionUtil;
import com.it.park.constant.Constant;
import com.it.park.model.ArticleType;
import com.it.park.model.ResourceCollection;
import com.it.park.model.ResourceDownloadRecord;
import com.it.park.model.ResourceFile;
import com.it.park.rms.client.model.User;
import com.it.park.util.file.FileUtil;

@Controller
@RequestMapping("/user/resource")
public class ResourceFileController {
	private String rootPath=Constant.USER_PATH;

	@Autowired
	private ResouceFileService resouceFileService;
	@Autowired
	private ResourceDownloadRecordService downloadRecordService;
	@Autowired
	private ResourceCollectionService resourceCollectionService;
	@Autowired
	private ArticleTypeService articleTypeService;
	/**
	 *个人资源显示列表页面
	 */
	@RequestMapping("resourceManage")
	public String resourceList(HttpServletRequest request,HttpSession session){
		User user = SessionUtil.getSessionUser(session);
		List<ResourceFile> resList = resouceFileService.selectByUserId(user.getId());
		request.setAttribute("resList", resList);
		return rootPath+"resourcefile/resourceManage";
	}
	
	/**
	 *上传资源页面
	 */
	@RequestMapping("toUploadResFile")
	public String toUploadResFile(HttpServletRequest request){
		return rootPath+"resourcefile/uploadResouce";
	}
	
	/**
	 * 文章类型跟资源类型一样
	 * @return
	 */
	
	@RequestMapping("listType")
	@ResponseBody
	public List<ArticleType> listType(HttpServletRequest request,HttpSession session){
		List<ArticleType> articleTypes = articleTypeService.selectAll();
		return articleTypes;
	}
	
	/**
	 *上传资源功能
	 */
	@RequestMapping("uploadResFile")
	public String uploadResFile(@RequestParam("file") MultipartFile file,ResourceFile rf,HttpServletRequest request,HttpSession session){
		if(!file.isEmpty()){
			try {
				String fileName = file.getOriginalFilename();
				InputStream is =  file.getInputStream();
				int index = fileName.lastIndexOf(".");
				String size = FileUtil.getFileSize(file.getSize());
				User user = SessionUtil.getSessionUser(session);
				String filePath = FileUtil.getResourceFilePath(request,user.getUserName(),fileName);
				file.transferTo(new File(filePath));
				rf.setFileType(fileName.substring(index+1));
				rf.setUploadDate(new Date());
				rf.setUserId(user.getId());
				rf.setResourceSize(size);
				rf.setIsPass(0);
				rf.setFilePath(filePath);
//				rf.setResourceFileContent(FileUtil.getBytes(is));
				resouceFileService.insertSelective(rf);	
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return "redirect:resourceManage";
	}
	
	/**
	 *个人资源删除功能
	 */
	@RequestMapping("delResource")
	public String delResource(@RequestParam("id") Integer id,HttpServletRequest request,HttpSession session){
		resouceFileService.deleteByPrimaryKey(id);
		return "redirect:resourceManage";
	}
	
	
	/**
	 *个人下载资源列表页面
	 */
	@RequestMapping("downloadRecordList")
	public String downloadRecordList(HttpServletRequest request,HttpSession session){
		User user = SessionUtil.getSessionUser(session);
		List<ResourceDownloadRecord> rdr = downloadRecordService.selectByUserId(user.getId());
		List<ResourceFile> resList = new ArrayList<ResourceFile>();
		for (ResourceDownloadRecord r : rdr) {
			ResourceFile rf = resouceFileService.selectByPrimaryKey(r.getResourceId());
			resList.add(rf);
		}
		request.setAttribute("resList", resList);
		return rootPath+"resourcefile/resourceDownloadRecordList";
	}
	
	/**
	 *个人收藏资源列表页面
	 */
	@RequestMapping("resourceCollectionList")
	public String resourceCollectionList(HttpServletRequest request,HttpSession session){
		User user = SessionUtil.getSessionUser(session);
		List<ResourceCollection> rdr = resourceCollectionService.selectByUserId(user.getId());
		List<ResourceFile> resList = new ArrayList<ResourceFile>();
		for (ResourceCollection r : rdr) {
			ResourceFile rf = resouceFileService.selectByPrimaryKey(r.getResourceId());
			resList.add(rf);
		}
		request.setAttribute("resList", resList);
		return rootPath+"resourcefile/resourceCollectionList";
	}
	
	/**
	 * 取消收藏
	 */
	@RequestMapping("cancelCollection")
	public String cancelCollection(Integer resId,HttpServletRequest request,HttpSession session){
		User user = SessionUtil.getSessionUser(session);
		ResourceCollection record = new ResourceCollection();
		record.setUserId(user.getId());
		record.setResourceId(resId);
		resourceCollectionService.cancelResource(record);
		return  "redirect:resourceCollectionList";
	}
}
